<HTML>
 
<!-- Mirrored from www.javapractices.com/apps/movies/javadoc/src-html/hirondelle/movies/LaunchApplication.html by HTTrack Website Copier/3.x [XR&CO'2010], Sun, 12 Jun 2011 17:30:19 GMT -->
<HEAD>
  <TITLE>
LaunchApplication.java
  </TITLE>
  <LINK REL ='stylesheet' TYPE='text/css' HREF='../../../highlight.css' TITLE='Style'>
 </HEAD>
 <BODY>
<PRE>
<span class='keyword'>package</span> hirondelle.movies;<a name=line.1></a>
<a name=line.2></a>
<span class='keyword'>import</span> hirondelle.movies.util.Util;<a name=line.3></a>
<span class='keyword'>import</span> java.io.IOException;<a name=line.4></a>
<span class='keyword'>import</span> java.util.logging.FileHandler;<a name=line.5></a>
<span class='keyword'>import</span> java.util.logging.Level;<a name=line.6></a>
<span class='keyword'>import</span> java.util.logging.Logger;<a name=line.7></a>
<span class='keyword'>import</span> java.util.logging.SimpleFormatter;<a name=line.8></a>
<span class='keyword'>import</span> java.awt.Font;<a name=line.9></a>
<span class='keyword'>import</span> javax.swing.plaf.FontUIResource;<a name=line.10></a>
<span class='keyword'>import</span> java.util.*;<a name=line.11></a>
<span class='keyword'>import</span> javax.swing.UIManager;<a name=line.12></a>
<span class='keyword'>import</span> hirondelle.movies.exception.ExceptionHandler;<a name=line.13></a>
<span class='keyword'>import</span> hirondelle.movies.login.LoginController;<a name=line.14></a>
<a name=line.15></a>
<span class='comment'>/**<a name=line.16></a>
 &lt;b&gt;Launch the application.&lt;/b&gt;<a name=line.17></a>
 <a name=line.18></a>
 &lt;P&gt; Perform any needed one-time startup operations.<a name=line.19></a>
  Ask the user for their login credentials, and then display the main window.<a name=line.20></a>
*/</span><a name=line.21></a>
<span class='keyword'>public</span> <span class='keyword'>final</span> <span class='keyword'>class</span> LaunchApplication {<a name=line.22></a>
<a name=line.23></a>
  <span class='comment'>/**<a name=line.24></a>
   Run the application.<a name=line.25></a>
   <a name=line.26></a>
   &lt;P&gt;Performs the following :<a name=line.27></a>
   &lt;ul&gt;<a name=line.28></a>
    &lt;li&gt;configure JDK logging : log at &lt;tt&gt;FINEST&lt;/tt&gt; level to a file named &lt;tt&gt;log.txt&lt;/tt&gt; in the application's home <a name=line.29></a>
    directory. Overwrite the log file each time the application is launched. (Many apps would prefer to make the log level <a name=line.30></a>
    sensitive to an environment property, or a user preference.)<a name=line.31></a>
    &lt;li&gt;configure a custom {@link ExceptionHandler} for uncaught exceptions<a name=line.32></a>
    &lt;li&gt;use the native look and feel, natural to the runtime operating system<a name=line.33></a>
    &lt;li&gt;sets the font for the application (12-point Verdana)<a name=line.34></a>
    &lt;li&gt;show the login screen (no real validation is actually performed)<a name=line.35></a>
   &lt;/ul&gt;<a name=line.36></a>
   <a name=line.37></a>
   &lt;P&gt;Some apps might also perform :<a name=line.38></a>
   &lt;ul&gt;<a name=line.39></a>
    &lt;li&gt;display a temporary 'splash' screen<a name=line.40></a>
    &lt;li&gt;confirm a database connection<a name=line.41></a>
   &lt;/ul&gt;<a name=line.42></a>
   <a name=line.43></a>
   &lt;P&gt;The Swing tutorial recommends the following style for application launch :<a name=line.44></a>
   &lt;PRE&gt; <a name=line.45></a>
javax.swing.SwingUtilities.invokeLater(new Runnable() {<a name=line.46></a>
  public void run() {<a name=line.47></a>
    createAndShowGUI();<a name=line.48></a>
  }<a name=line.49></a>
});<a name=line.50></a>
  &lt;/PRE&gt;<a name=line.51></a>
   However, that style is not used here, since it doesn't seem necessary. During launch, <a name=line.52></a>
   no GUI has yet been realized, so it seems practically impossible for a thread to interact <a name=line.53></a>
   with it. <a name=line.54></a>
  */</span><a name=line.55></a>
  <span class='keyword'>public</span> <span class='keyword'>static</span> <span class='keyword'>void</span> main(String... aArgs){  <a name=line.56></a>
    configureJDKLogging();<a name=line.57></a>
    <a name=line.58></a>
    fLogger.config(<span class='literal'>"Launching application..."</span>);<a name=line.59></a>
    fLogger.config(<span class='literal'>"Operating System : "</span> + System.getProperty(<span class='literal'>"os.name"</span>)  + <span class='literal'>" "</span> +  System.getProperty(<span class='literal'>"os.version"</span>));<a name=line.60></a>
    fLogger.config(<span class='literal'>"Java Version: "</span> + System.getProperty(<span class='literal'>"java.version"</span>));<a name=line.61></a>
    fLogger.config(<span class='literal'>"Java Home: "</span> + System.getProperty(<span class='literal'>"java.home"</span>));<a name=line.62></a>
<a name=line.63></a>
    useCustomExceptionHandler();<a name=line.64></a>
    useNativeLookAndFeel();<a name=line.65></a>
    setApplicationFont();<a name=line.66></a>
    <a name=line.67></a>
    fLogger.config(<span class='literal'>"Showing user login screen."</span>);<a name=line.68></a>
    userLogin();<a name=line.69></a>
    <a name=line.70></a>
    fLogger.config(<span class='literal'>"Launch thread now ending."</span>);<a name=line.71></a>
  }<a name=line.72></a>
  <a name=line.73></a>
  <span class='comment'>/** <a name=line.74></a>
  Defines the name of the app.<a name=line.75></a>
  Could be used in an About box, trouble ticket emails, and so on.<a name=line.76></a>
  */</span><a name=line.77></a>
  <span class='keyword'>public</span> <span class='keyword'>static</span> <span class='keyword'>final</span> String APP_NAME = <span class='literal'>"My Movies"</span>;<a name=line.78></a>
  <a name=line.79></a>
  <span class='comment'>/** The version of the app. An arbitrary string. */</span><a name=line.80></a>
  <span class='keyword'>public</span> <span class='keyword'>static</span> <span class='keyword'>final</span> String APP_VERSION = <span class='literal'>"1.0.0"</span>;<a name=line.81></a>
  <a name=line.82></a>
  <span class='comment'>// PRIVATE //<a name=line.83></a>
</span> <a name=line.84></a>
  <span class='comment'>/** Makes no sense to call this class's constructor, so it's made private. */</span><a name=line.85></a>
  <span class='keyword'>private</span> LaunchApplication(){ }<a name=line.86></a>
  <span class='keyword'>private</span> <span class='keyword'>static</span> <span class='keyword'>final</span> Logger fLogger = Util.getLogger(LaunchApplication.<span class='keyword'>class</span>);<a name=line.87></a>
<a name=line.88></a>
  <span class='comment'>/**<a name=line.89></a>
   Set up JDK logging to emit logging entries to a file in the application's<a name=line.90></a>
   installation directory. The file name is log.txt.<a name=line.91></a>
  */</span><a name=line.92></a>
  <span class='keyword'>private</span> <span class='keyword'>static</span> <span class='keyword'>void</span> configureJDKLogging() {<a name=line.93></a>
    <span class='keyword'>boolean</span> OVERWRITE = <span class='keyword'>false</span>;<a name=line.94></a>
    <span class='keyword'>int</span> SINGLE_FILE = <span class='literal'>1</span>;<a name=line.95></a>
    <span class='keyword'>int</span> UNLIMITED_SIZE = <span class='literal'>0</span>;<a name=line.96></a>
    <span class='keyword'>try</span> {<a name=line.97></a>
      FileHandler fileHandler = <span class='keyword'>new</span> FileHandler(<span class='literal'>"log.txt"</span>,  UNLIMITED_SIZE, SINGLE_FILE, OVERWRITE);<a name=line.98></a>
      fileHandler.setLevel(Level.FINEST);<a name=line.99></a>
      fileHandler.setFormatter(<span class='keyword'>new</span> SimpleFormatter());<a name=line.100></a>
      fLogger.addHandler(fileHandler);<a name=line.101></a>
    }<a name=line.102></a>
    <span class='keyword'>catch</span> (IOException ex){<a name=line.103></a>
      fLogger.severe(<span class='literal'>"Cannot set up log file."</span>);<a name=line.104></a>
    }<a name=line.105></a>
  }<a name=line.106></a>
  <a name=line.107></a>
  <span class='comment'>/**   See {@link ExceptionHandler}.    */</span><a name=line.108></a>
  <span class='keyword'>private</span> <span class='keyword'>static</span> <span class='keyword'>void</span> useCustomExceptionHandler(){<a name=line.109></a>
    fLogger.config(<span class='literal'>"Setting up custom exception handler"</span>);<a name=line.110></a>
    System.setProperty(<span class='literal'>"sun.awt.exception.handler"</span>, ExceptionHandler.<span class='keyword'>class</span>.getName());    <a name=line.111></a>
  }<a name=line.112></a>
  <a name=line.113></a>
   <span class='comment'>/**<a name=line.114></a>
    Use the look which is usually used on the current operating system.<a name=line.115></a>
    <a name=line.116></a>
    &lt;P&gt;For example, on a Windows machine, use a Windows look and feel.<a name=line.117></a>
    If you are using a non-default look and feel, then you should set it first thing.<a name=line.118></a>
    For more info, see the <a name=line.119></a>
    &lt;a href='http://java.sun.com/docs/books/tutorial/uiswing/lookandfeel/plaf.html#available'&gt;Swing Tutorial&lt;/a&gt;<a name=line.120></a>
    <a name=line.121></a>
    &lt;P&gt;Warning: on Windows, the native look & feel displays accelerator keys only when ALT key is held down. <a name=line.122></a>
    Otherwise, they are not displayed!<a name=line.123></a>
  */</span><a name=line.124></a>
  <span class='keyword'>private</span> <span class='keyword'>static</span> <span class='keyword'>void</span> useNativeLookAndFeel() {<a name=line.125></a>
    <span class='keyword'>try</span> {<a name=line.126></a>
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());<a name=line.127></a>
    }<a name=line.128></a>
    <span class='keyword'>catch</span>(Throwable ex){<a name=line.129></a>
      fLogger.severe(<span class='literal'>"Cannot set the look and feel."</span>);<a name=line.130></a>
    }<a name=line.131></a>
  }<a name=line.132></a>
<a name=line.133></a>
  <span class='keyword'>private</span> <span class='keyword'>static</span> <span class='keyword'>void</span> setApplicationFont() {<a name=line.134></a>
    FontUIResource fontResource = <span class='keyword'>new</span> FontUIResource(<span class='literal'>"Verdana"</span>,Font.PLAIN,<span class='literal'>12</span>);<a name=line.135></a>
    Enumeration keys = UIManager.getDefaults().keys();<a name=line.136></a>
    <span class='keyword'>while</span> (keys.hasMoreElements()) {<a name=line.137></a>
      Object key = keys.nextElement();<a name=line.138></a>
      Object value = UIManager.get (key);<a name=line.139></a>
      <span class='keyword'>if</span> (value <span class='keyword'>instanceof</span> FontUIResource)<a name=line.140></a>
        UIManager.put (key, fontResource);<a name=line.141></a>
      }<a name=line.142></a>
  }<a name=line.143></a>
  <a name=line.144></a>
  <span class='comment'>/**<a name=line.145></a>
    Show the login screen. <a name=line.146></a>
    <a name=line.147></a>
    &lt;P&gt;This is the first GUI element displayed to the end user.<a name=line.148></a>
    When the GUI is realized, then all interaction with the GUI must be through the <a name=line.149></a>
    Event Dispatch thread.<a name=line.150></a>
    <a name=line.151></a>
    &lt;P&gt;For more info, see<a name=line.152></a>
    &lt;a href='http://www.javapractices.com/topic/TopicAction.do?Id=153'&gt;this article&lt;/a&gt;.<a name=line.153></a>
   */</span><a name=line.154></a>
   <span class='keyword'>private</span> <span class='keyword'>static</span> <span class='keyword'>void</span> userLogin(){<a name=line.155></a>
     fLogger.config(<span class='literal'>"Showing the login screen."</span>);<a name=line.156></a>
     LoginController login = <span class='keyword'>new</span> LoginController();<a name=line.157></a>
     login.askUserForCredentials();<a name=line.158></a>
   }<a name=line.159></a>
}<a name=line.160></a>
 <a name=line.161></a>
</PRE><a name=line.162></a>
 </BODY><a name=line.163></a>
<HTML><a name=line.164></a>
